package com.hyc.myvideo.ui.activity.column;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Column;
import com.hyc.myvideo.ui.activity.home.AudioActivity;
import com.hyc.myvideo.ui.activity.my.LoginActivity;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.utils.imguitl.ImgUitl;
import com.hyc.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 专栏内容
 * Created by dfeads on 2017/7/11.
 */

public class ColumnActivity extends BaseFActivity {

  /** 头像 */
  @BindView(R.id.activity_column_head) ImageView mHead;
  /** 顶部标题 */
  @BindView(R.id.activity_column_title) TextView mTitle;
  /** 认证 */
  @BindView(R.id.activity_column_auth) TextView mAuth;
  /** 简介 */
  @BindView(R.id.activity_column_text) TextView mText;
  /** 下面标题 */
  @BindView(R.id.activity_column_text_title) TextView mTextTitle;
  /** 内容 */
  @BindView(R.id.activity_column_content) TextView mContent;
  /** 报名 */
  @BindView(R.id.activity_column_sign_up) Button mSignUp;

  private String column_id = "";
  private Column column = null;

  @Override public int getContentViewId() {
    return R.layout.activity_column;
  }

  @Override protected void initView() {
    setLeftBtnClick(true, getStr(R.string.column));
    setRightClick(R.mipmap.home_practice, new View.OnClickListener() {
      @Override public void onClick(View view) {
        startActivity(new Intent(ColumnActivity.this, AudioActivity.class));
      }
    });

    column_id = getIntent().getStringExtra("id");
    if (!StringUtils.isEmpty(column_id)) {
      api.getColumnDetails(column_id);
    }
  }

  @Override protected void initData() {
  }

  @Override protected void oNext(String resulte, String method) {
    if (method.equals(Constants.API_COLUMN_DETAILS)) {
      BaseResult<Column> columnB =
          JSONObject.parseObject(resulte, new TypeReference<BaseResult<Column>>() {
          });
      if (columnB.isCode()) {
        column = columnB.getResult();
        setView();
      } else {
        Toaster.show(columnB.getMsg());
      }
    } else if (method.equals(Constants.API_COLUMN_ENROLL)) {
      BaseResult<String> base =
          JSONObject.parseObject(resulte, new TypeReference<BaseResult<String>>() {
          });
      Toaster.show(base.getMsg());
    }
  }

  @Override protected void oError() {

  }

  private void setView() {
    if (column == null) {
      return;
    }

    ImgUitl.setImg(column.getColumn_cover(), mHead);
    mTitle.setText(column.getColumn_title());
    mTextTitle.setText(column.getColumn_title());
    mText.setText(column.getColumn_describe());
    mContent.setText(column.getColumn_content());
  }

  @OnClick(R.id.activity_column_sign_up) public void onViewClicked(View v) {
    switch (v.getId()) {
      case R.id.activity_column_sign_up:
        if (userInfo.isLogin()) {
          api.postColumnSignUp(userInfo.getUser_id(), column_id);
        } else {
          startActivity(new Intent(ColumnActivity.this, LoginActivity.class));
        }
        break;
    }
  }
}
